package com.biaobiaoqi.scoreanalysis.serviceImpl;

import javax.annotation.Resource;

import com.biaobiaoqi.scoreanalysis.exception.DuplicateRelation;
import com.biaobiaoqi.scoreanalysis.exception.NoRelationFoundException;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Clazz;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Course;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.Teacher;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.TeacherClazzCourseRelation;
import com.biaobiaoqi.scoreanalysis.service.IClazzService;
import com.biaobiaoqi.scoreanalysis.service.ICourseService;
import com.biaobiaoqi.scoreanalysis.service.ITeacherClazzCourseRelationService;
import com.biaobiaoqi.scoreanalysis.service.ITeacherService;

public class TeacherClazzCourseRelationServiceImpl extends
		DaoServiceImpl<TeacherClazzCourseRelation> implements
		ITeacherClazzCourseRelationService {
	@Resource
	ITeacherService teacherService;
	@Resource
	IClazzService clazzService;
	@Resource
	ICourseService courseService;

	@Override
	public void addRelation(int TeacherId, int clazzId, int CourseId)
			throws DuplicateRelation {
		if (isRelationExist(TeacherId, clazzId, CourseId)) {
			throw new DuplicateRelation();
		}
		Teacher teacher = teacherService.findTeacher(TeacherId);
		Clazz clazz = clazzService.findClazz(clazzId);
		Course course = courseService.findCourse(CourseId);
		TeacherClazzCourseRelation relation = new TeacherClazzCourseRelation();
		relation.setClazz(clazz);
		relation.setCourse(course);
		relation.setTeacher(teacher);
		getDao().create(relation);
	}

	@Override
	public void deleteRelation(int TeacherId, int clazzId, int CourseId)
			throws NoRelationFoundException {
		TeacherClazzCourseRelation relation = findRelation(TeacherId, clazzId,
				CourseId);
		if (relation == null) {
			throw new NoRelationFoundException();
		}
		getDao().delete(relation);
	}

	@Override
	public void deleteRelation(int relationId) throws NoRelationFoundException {
		TeacherClazzCourseRelation relation = findRelation(relationId);
		if (relation == null) {
			throw new NoRelationFoundException();
		} else {
			getDao().delete(relation);
		}
	}

	@Override
	public TeacherClazzCourseRelation findRelation(int TeacherId, int clazzId,
			int CourseId) {
		Teacher teacher = teacherService.findTeacher(TeacherId);
		Clazz clazz = clazzService.findClazz(clazzId);
		Course course = courseService.findCourse(CourseId);
		return getDao().queryUnique(
						"from TeacherClazzCourseRelation t where t.teacher=? and t.clazz=? and t.course=?",
						teacher, clazz, course);
	}

	@Override
	public TeacherClazzCourseRelation searchRelation() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isRelationExist(int TeacherId, int clazzId, int CourseId) {
		TeacherClazzCourseRelation relation = findRelation(TeacherId, clazzId, CourseId);
		if (relation==null) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public TeacherClazzCourseRelation findRelation(int relationId) {
		return getDao().findById(TeacherClazzCourseRelation.class, relationId);
	}
}
